package com.yc154.web;

import com.yc.damai.entity.User;
import com.yc.damai.util.QRCodeUtil;
import com.yc.damai.vo.Result;
import com.yc.starter.login.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.servlet.ModelAndView;
import reactor.core.publisher.Mono;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

@RequestMapping("autoLogin")
@RestController
public class AutoLoginDo {

    ////////////////////////////// 扫码登录 ////////////////////////////////
    @RequestMapping(path = "qrcode", produces = MediaType.IMAGE_PNG_VALUE)
    public void qrcode(String content, OutputStream output) throws Exception {
        QRCodeUtil.encode(content, "e:/who.png", output, true);
    }

    @Autowired
    JdbcTemplate jdbcTemplate;

    @RequestMapping("login")
    public Mono<Result> login(int uid, HttpServletResponse response) {
        Map user = jdbcTemplate.queryForMap("select * from user where uid=?",uid);
        Result result = Result.ok("登录成功", user);
        String jwt = JwtUtils.createJWT(30, user);
        response.setContentType(MediaType.APPLICATION_JSON_UTF8.toString());
        response.addHeader("Authorization", "Bearer " + jwt);
        // 这不是正真的响应式编程代码
        return Mono.just(result);
    }

    private static HashMap<String,Integer> redis = new HashMap<>();

    // 打开二维码
    @RequestMapping(path = "askAutoLogin", produces = MediaType.IMAGE_PNG_VALUE)
    public void askAutoLogin(OutputStream output, HttpServletResponse response) throws Exception {
        String uuid = UUID.randomUUID().toString();
        // 将uuid发送至页面
        Cookie cookie = new Cookie("uuid", uuid);
        // httpOnly 默认 false, 可以不写, 问题与该设置无关
        cookie.setHttpOnly(false);
        // 漏写了 Path
        cookie.setPath("/");
        response.addCookie(cookie);
        // redis 可以设置超时
        redis.put(uuid,0);
        String content = "http://192.168.1.118/user/autoLogin/toConfirm?uuid=" + uuid;
        System.out.println("askAutoLogin: " + content);
        qrcode(content, output);
    }

    // 手机扫描 => 确认登录
    @RequestMapping("toConfirm")
    public ModelAndView toConfirm(String uuid, ModelAndView mav){
        System.out.println("toConfirm: http://192.168.1.118/cloud-mobile/confirm.html?uuid="+ uuid);
        mav.setViewName("redirect:http://192.168.1.118/cloud-mobile/confirm.html?uuid="+ uuid);
        return mav;
    }

    // 手机授权登录
    @RequestMapping("allowAutoLogin")
    public Result allowAutoLogin(Integer uid, String uuid){
        if(uid == null) {
            return Result.fail("请先登录系统");
        }
        redis.put(uuid, uid);
        return Result.ok("扫码成功!");
    }

    // PC轮询
    @RequestMapping("canAutoLogin")
    public Result canAutoLogin(String uuid){
        Integer uid = redis.get(uuid);
        if(uid == null){
            return Result.fail("二维码已过期!");
        } else if(uid == 0){
            return Result.fail("二维码未扫描!");
        }
        return Result.ok("扫码成功, 运行登录!", uid);
    }
}
